import {
  Column,
  Entity,
  ManyToMany,
  Tree,
  TreeChildren,
  TreeParent,
} from 'typeorm';
import { DBColumns } from '../';
import { MenusSchema, RoleSchema } from '../../schema';
import { Role } from '../role/role';

@Entity('menus')
@Tree('closure-table')
export class Menus extends DBColumns implements MenusSchema {
  @TreeParent()
  parent?: MenusSchema;

  @TreeChildren()
  children?: MenusSchema[];

  @ManyToMany(() => Role, role => role.menus)
  role?: RoleSchema[];

  @Column({
    type: 'varchar',
    length: 255,
    nullable: false,
    default: null,
    comment: '路由访问路径',
  })
  path: string;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: false,
    default: null,
    comment: '路由名称（唯一）',
  })
  name: string;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: true,
    default: null,
    comment: '路由重定向地址',
  })
  redirect?: string;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: true,
    default: null,
    comment: '视图文件路径',
  })
  component?: string;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: false,
    default: null,
    comment: '菜单和面包屑对应的图标',
  })
  icon: string;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: false,
    default: null,
    comment: '路由标题',
  })
  title: string;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: true,
    default: null,
    comment: '是否在菜单中隐藏, 需要高亮的 path (通常用作详情页高亮父级菜单)',
  })
  activeMenu?: string;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: true,
    default: null,
    comment: '路由外链时填写的访问地址',
  })
  isLink?: string;

  @Column({
    type: 'bool',
    length: 0,
    nullable: true,
    default: false,
    comment: '是否在菜单中隐藏',
  })
  isHide?: boolean;

  @Column({
    type: 'bool',
    length: 0,
    nullable: true,
    default: false,
    comment: '菜单是否全屏',
  })
  isFull?: boolean;

  @Column({
    type: 'bool',
    length: 0,
    nullable: true,
    default: false,
    comment: '菜单是否固定在标签页中',
  })
  isAffix?: boolean;

  @Column({
    type: 'bool',
    length: 0,
    nullable: true,
    default: true,
    comment: '当前路由是否缓存',
  })
  isKeepAlive?: boolean;

  @Column({ type: 'int', nullable: false, default: 99, comment: '菜单序号' })
  sort?: number;
}
